<?php
class CMRPageHome extends CMRPage
{
	protected function getContents()
	{
		$html = "<td>";
		$html .= "<h1>Approve Resources</h1>";

		
//		echo $this->getSubAction();
		
		switch ($this->getSubAction())
		{
			case 'UpdateActions':
				$html .= $this->getUpdateActions();
				break;
			default:
				$html .= $this->getDefault();			
				break;
		}
	
		$html .= "</td>";
		return $html;
				
	}
	private function getDefault()
	{
		$html = "<p>The following documents have not yet been approved by you.</p>";
		$script = new EditDeleteFormScript();
		$html = $script->getHTML();
		
		
		$datatable = new DataTable();
		$datatable->setParentPage($this);
		$datatable->setName("ApproveResources");
		$datatable->setValue("ApproveResources");
		$datatable->setViewable(true);
		$datatable->setEditable(false);
		$datatable->setDeletable(false);
		$datatable->setEditDeleteType("Hyperlink");
		
		$datatable->setDBTableName("Resources");
		$datatable->setDatabase($this->Database);
		$theRenderer = new DataTableRendererEdit($datatable);
		$theRenderer->setAction = "ListEvents";
		$theRenderer->setSubAction("Approve");

		$datatable->setRenderer($theRenderer);
		$columns = array();
		$columns[] = new DataTableColumn("ResourceTitle","Title","");
		$columns[] = new DataTableColumn("ResourceDescription","Description","");
		$columns[] = new DataTableColumn("ResourceTypeID","Type","LongNameEnumeration");
//		$columns[] = new DataTableColumn("StatusID","Status","LongNameEnumeration");
//		$columns[] = new DataTableColumn("UserID","Submitted By","LongNameEnumeration");
		$columns[] = new DataTableColumn("StatusID","Approved By","BodgeMultiEnumeration");
		$datatable->setColumns($columns);
		//$datatable->setSelect("select ResourceID as ColumnID, ResourceTitle, ResourceDescription, ResourceTypeID FROM Resources");
//		$datatable->setSelect("SELECT res.ResourceID as ColumnID, res.ResourceTitle, res.ResourceDescription, res.ResourceTypeID, act.ActionDate, usr.LongName FROM (Resources as res INNER JOIN Actions as act ON res.ResourceID = act.ResourceID)INNER JOIN Users as usr ON act.ActionUserID = usr.UserID ");	

//		$datatable->setSelect("SELECT res.ResourceID as ColumnID, res.ResourceTitle, res.ResourceDescription, res.ResourceTypeID, stat.StatusID FROM Resources as res INNER JOIN ResourceStatuss as stat ON res.ResourceID = stat.ResourceID");	

		$datatable->addORCondition("stat.StatusID = 2");
		$datatable->setOrderBy("ResourceTitle");

		$html .= $datatable->getHTML();
		return $html;
	}

	private function getUpdateActions()
	{
		// update approve action
		$date = new DateTime();
		$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, UserID)
		VALUES ('".$_POST['ResourceID']."','2','".$date->format('Y-m-d H:i:s')."','".$_POST['UserID']."')";
//		echo $sql."<br>";
		$this->Database->ExecuteSQL($sql);
		$html = "<p>Resource approved</p>";
		
		// can it go live??
		$sql = "SELECT id.UserID, n.ShortName FROM Actions as id INNER JOIN Users as n ON id.UserID = n.UserID WHERE id.ResourceID = ".$_POST['ResourceID']." AND id.ActionTypeID = 2";
		//				echo $sql;
		$this->Database->ExecuteSQL($sql);
//		echo $this->Database->getNumRows()."<br>\n";
		if($this->Database->getNumRows() >= $this->NeededForApproval)
		{
//			$sql = "UPDATE ResourceStatuss SET StatusID='1', Date='".$date->format('Y-m-d H:i:s')."' WHERE ResourceID='".$_POST['ResourceID']."'";
			$this->Database->ExecuteSQL($sql);
		}		
		
		return $html;
	}
	
}
?>